<?php
namespace App\Services\Fund\CurrencySplitLog;
use App\Services\Fund\ValidateCurrencySplitLog;
use App\Models\Fund\CurrencySplitLog;
use Illuminate\Support\Facades\DB;
class CurrencySplitLogLogic
{
    /*补全日志记录信息*/
   /* protected function _replenishOptionLogInfo($logInfo, \app\serverfund\model\CurrencyAccount $userInfo): array 
    {
        $logInfo['total'] = $userInfo['total'];
        $logInfo['income'] = $userInfo['income'];
        $logInfo['expend'] = $userInfo['expend'];
        $logInfo['available_currency'] = $userInfo['available_currency'];
        $logInfo['split'] = $userInfo['split'];
        $logInfo['freeze'] = $userInfo['freeze'];
        $logInfo['lock'] = $userInfo['lock'];
        return $logInfo;
    }*/

    /*添加单个或多个日志记录*/
  /*  protected function _saveOptionLog(...$logs): int 
    {
        $addRes = $this->model($this->currencyOptionLogModel)->saveAll($logs);
        return $this->model($this->currencyOptionLogModel)->getLastInsID();
    }*/

    /*拆分亚数币*/
   /* public function split($data): array 
    {
        //修改该用户数字币账户信息后的查询条件
        $whereFindUser['user_id'] = $data['user_id'];
        $whereFindUser['available_currency'] = ['egt', 0]; 
        //修改数字币账户  
        $saveUserAccount['available_currency'] = ['exp', 'available_currency-' . $data['currency']];
        $saveUserAccount['split'] = ['exp', 'split+' . $data['currency']];
        $saveUserAccount['total'] = ['exp', 'total-' . $data['currency']];
        $saveUserAccount['expend'] = ['exp', 'expend+' . $data['currency']];
        $saveUserAccount['update_ip'] = $data['update_ip'];
        //添加数字币操作日志记录
        $addOptionLog['user_id'] = $data['user_id'];
        $addOptionLog['app_id'] = $data['app_id'];
        $addOptionLog['type'] = 'split';
        $addOptionLog['currency'] = $data['currency'];
        $addOptionLog['remark'] = '用户['. $data['user_id'] . ']拆分亚数币数值' . $data['currency'];
        $addOptionLog['create_ip'] = $data['update_ip'];
        $addOptionLog['update_ip'] = $data['update_ip'];
        $addOptionLog['status'] = 1;
        $addOptionLog['user_info'] = $data['user_info'];
        $addOptionLog['opposite_info'] = $data['opposite_info'];
        //添加数字币拆分记录
        $addSplitLog['user_id'] = $data['user_id'];
        $addSplitLog['app_id'] = $data['app_id'];
        $addSplitLog['currency'] = $data['currency'];
        $addSplitLog['public_key'] = password_hash(microtime(), PASSWORD_DEFAULT);
        $addSplitLog['lock_private_hash'] = password_hash($data['lock_key'], PASSWORD_DEFAULT);
        $addSplitLog['get_private_hash'] = password_hash($data['get_key'], PASSWORD_DEFAULT);
        $addSplitLog['create_ip'] = $data['update_ip'];
        $addSplitLog['update_ip'] = $data['update_ip'];
        $addSplitLog['status'] = 0;
        $addSplitLog['user_info'] = $data['user_info'];
        $addSplitLog['opposite_info'] = $data['opposite_info'];

        $this->db()->startTrans();
        try{
            //修改数字币账户
            $updateUserRes = $this->model($this->currencyAccountModel)->save($saveUserAccount, $whereFindUser);
            //查询数字币修改后的账户
            $userInfo = $this->model($this->currencyAccountModel)->lock(true)->where($whereFindUser)->find();
            if(is_null($userInfo)){  
                return verify_err(null, '用户不存在或可用亚数币不足');
            }
            //补全日志记录信息并添加日志记录
            $addOptionLog = $this->_replenishOptionLogInfo($addOptionLog, $userInfo);
            $addSplitLog['currency_log_id'] = $this->_saveOptionLog($addOptionLog);
            //添加数字币拆分记录
            $addSplitRes = $this->model($this->currencySplitLogModel)->isUpdate(false)->save($addSplitLog);
            $this->db()->commit();
        }catch(\Exception $e){
            $this->db()->rollback();
            return server_err(null, '数据库异常:' . $e->getMessage()); 
        }

        return auto_success($addSplitRes, '拆分数字币成功');
    }*/

    /*交易方锁定亚数币*/
    /*public function lock($data): array 
    {
        $whereFindUser['user_id'] = $data['user_id'];
        //验证公钥和锁定私钥
        $whereFindKey['public_key'] = $data['public_key'];
        $whereFindKey['status'] = 0;
        //修改被锁定数字币的账户信息
        $updateOppositeUser['update_ip'] = $data['update_ip'];
        //补全操作记录并添加数字币操作记录
        $addOptionLog['user_id'] = $data['user_id'];
        $addOptionLog['app_id'] = $data['app_id'];
        $addOptionLog['type'] = 'lock';
        $addOptionLog['create_ip'] = $data['update_ip'];
        $addOptionLog['update_ip'] = $data['update_ip'];
        $addOptionLog['user_info'] = $data['user_info'];
        $addOptionLog['opposite_info'] = $data['opposite_info'];
        //修改拆分记录表
        $updateSplitLog['opposite_id'] = $data['user_id'];
        $updateSplitLog['app_id'] = $data['app_id'];
        $updateSplitLog['update_ip'] = $data['update_ip'];
        $updateSplitLog['status'] = 1;
        $updateSplitLog['opposite_info'] = $data['user_info'];
        $this->db()->startTrans();
        try{
            $findKey = $this->model($this->currencySplitLogModel)->where($whereFindKey)->find();
            if(is_null($findKey)){
                return verify_err(null, '没有找到符合条件的亚数币拆分记录');
            }
            if(!password_verify($data['lock_key'], $findKey['lock_private_hash'])){
                return verify_err(null, '锁定私钥错误');
            }
            $whereUpdateUser['user_id'] = $findKey['user_id'];
            $updateOppositeUser['split'] = ['exp', '`split`-' . $findKey['currency']];
            $updateOppositeUser['lock'] = ['exp', '`lock`+' . $findKey['currency']];
            //修改被锁定数字币的账户信息
            $updateUserRes = $this->model($this->currencyAccountModel)->isUpdate(true)->save($updateOppositeUser, $whereUpdateUser);
            $userInfo = $this->model($this->currencyAccountModel)->where($whereFindUser)->find();
            //补全操作记录并添加数字币操作记录
            $addOptionLog['opposite_id'] = $findKey['user_id'];
            $addOptionLog['currency'] = $findKey['currency'];
            $addOptionLog['remark'] = "用户[" . $data['user_id'] . "]锁定亚数币数值" . $findKey['currency'];
            $addOptionLog = $this->_replenishOptionLogInfo($addOptionLog, $userInfo);
            $updateSplitLog['currency_log_id'] = $this->_saveOptionLog($addOptionLog);
            $updateSplitLog['currency'] = $findKey['currency'];
            //修改数字币拆分记录表
            $whereUpdateSplit['id'] = $findKey['id'];
            $updateSplitRes = $this->model($this->currencySplitLogModel)->save($updateSplitLog, $whereUpdateSplit);

            $this->db()->commit();
        }catch(\Exception $e){
            $this->db()->rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }

        return auto_success($updateSplitLog, '成功锁定亚数币数值[' . $updateSplitLog['currency'] . ']');
    }*/

    /*交易方获取自己已锁定的亚数币*/
    /*public function read($data): array 
    {
        //验证获取私钥
        $whereFindKey['public_key'] = $data['public_key'];
        $whereFindKey['opposite_id'] = $data['is_lock'] ? $data['user_id'] : 0;
        $whereFindKey['status'] = $data['is_lock'] ? 1 : 0;
        //修改数字币账户
        $whereReadUser['user_id'] = $data['user_id'];
        $whereReadUser['status'] = 1;
        //补全信息并添加操作记录
        $addOptionLog['user_id'] = $data['user_id'];
        $addOptionLog['app_id'] = $data['app_id'];
        $addOptionLog['type'] = 'get';
        $addOptionLog['create_ip'] = $data['update_ip'];
        $addOptionLog['update_ip'] = $data['update_ip'];
        $addOptionLog['user_info'] = $data['user_info'];
        $addOptionLog['opposite_info'] = $data['opposite_info'];
        //修改拆分记录
        $updateSplitLog['update_ip'] = $data['update_ip'];
        $updateSplitLog['status'] = 2;
        $updateSplitLog['opposite_info'] = $data['user_info'];
        $this->db()->startTrans();
        try{
            //验证钥匙
            $findKey = $this->model($this->currencySplitLogModel)->where($whereFindKey)->find();
            if(is_null($findKey)){
                return verify_err(null, '没有找到符合条件的亚数币拆分记录');
            }
            if(!password_verify($data['get_key'], $findKey['get_private_hash'])){
                return verify_err(null, '获取私钥错误');
            }
            //修改原拥有人数字币账户 和 获取人数字币账户
            $whereOriginUser['user_id'] = $findKey['user_id'];
            $whereOriginUser['status'] = 1;
            $updateOrigin['lock'] = ['exp', '`lock`-' . $findKey['currency']];
            $updateOrigin['update_ip'] = $data['update_ip'];
            $updateRead['total'] = ['exp', 'total+' . $findKey['currency']];
            $updateRead['income'] = ['exp', 'income+' . $findKey['currency']];
            $updateRead['available_currency'] = ['exp', 'available_currency+' . $findKey['currency']];
            $updateRead['update_ip'] = $data['update_ip'];

            $updateOriginRes = $this->model($this->currencyAccountModel)->isUpdate(true)->save($updateOrigin, $whereOriginUser);
            $updateReadRes = $this->model($this->currencyAccountModel)->isUpdate(true)->save($updateRead, $whereReadUser);
            //添加日志操作记录
            $readUserInfo = $this->model($this->currencyAccountModel)->where($whereReadUser)->find();
            $addOptionLog['opposite_id'] = $findKey['user_id'];
            $addOptionLog['currency'] = $findKey['currency'];
            $addOptionLog['remark'] = '用户[' . $data['user_id'] . ']成功获取用户[' . $findKey['user_id'] . ']亚数币数值' . $findKey['currency'];
            $addOptionLog = $this->_replenishOptionLogInfo($addOptionLog, $readUserInfo);
            $updateSplitLog['currency_log_id'] = $this->_saveOptionLog($addOptionLog);
            //修改拆分记录
            $whereUpdateSplit['id'] = $findKey['id'];
            $updateSplitRes = $this->model($this->currencySplitLogModel)->isUpdate(true)->save($updateSplitLog, $whereUpdateSplit);

            $this->db()->commit();

        }catch(\Exception $e){

            $this->db()->rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());

        }
        return auto_success($updateSplitLog, '成功获取亚数币数值[' . $findKey['currency'] . ']');
    }*/

    /*获取数字币拆分记录*/
    public function list($data): array 
    {
        $where['status'] = 1;
        $res = CurrencySplitLog::where($where)
                     ->orderBy('id', 'desc')
                     ->paginate($data['list_rows'], null, null, $data['page'])
                     ->toArray();  
        return auto_return_page($res);
    }

    /*获取指定用户的拆分记录*/
    public function listByUserIds(array $data): array
    {
        $res = CurrencySplitLog::whereIn('user_id',[$data['user_ids']])
                     ->orderBy('id', 'desc')
                     ->paginate($data['list_rows'], null, null, $data['page'])
                     ->toArray();  
        return auto_return_page($res);
    }

    //加入状态的筛选
    public function listByUserStatus(array $data): array
    {
    
        $where['status'] = $data['status'];
        $res = CurrencySplitLog::where($where)
                     ->whereIn('user_id',[$data['user_id']])
                     ->orderBy('id', 'desc')
                     ->paginate($data['list_rows'], null, null, $data['page'])
                     ->toArray();              
        return auto_return_page($res);
    }
}